<?php
namespace Home\Controller;
use Think\Controller;
use Think\View;

<<<<<<< HEAD
class CourseController extends Controller { //邹佳敏
=======
class CourseController extends CommonController { //邹佳敏
>>>>>>> 62e237362e20a94a945bc25068926c792b5c027b

    public function courseList(){

        $Course = M('course');
        $this->courseList = $Course->join('club ON club.club_id = course.course_club_id')->select();
        $cnt = 1;
        $this->assign('cnt', $cnt);
        $this->display();
    }

    //显示添加课程页面
    public function courseAdd(){

        //获取所有俱乐部列表
        $Club = M('club');
        $clubList = $Club->select();
        $this->assign('clubList',$clubList);

        $this->display();
    }

    //添加课程
    /*
     * 还需完善：拿到登陆俱乐部管理员所属俱乐部的club_id（在session里），需要放入数据表
     * */
    public function doCourseAdd()
    {
        $Course = D("course");

        if (!$Course->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Course->getError());
        } else {
            // 创建成功，做其他事情
            if ($Course->create()) {
                $pname = 'default.jpg';
                if($_FILES['file']['error'] == 0){ //上传了文件
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                }
            }
            $Course->course_photo = $pname;
            if ($Course->add()) {
                $this->success('添加成功', './courseList', 2);
            } else {
                $this->error("添加失败");
            }
        }
    }

    //删除课程信息
    public function courseDel($courseId)
    {

        $Course = M('course');

        $photoName = ($Course->select($courseId))['0']['course_photo'];
        if ($photoName != 'default.jpg') { //如果不是默认文件
            unlink('./Public/upload/coursepics/' . $photoName); //删除对应文件
        }
        if ($Course->delete($courseId)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //显示课程详情+修改课程信息
    public function courseMod($courseId)
    {

        $Course = D('course');

        $course_club_id = ($Course->select($courseId))['0']['course_club_id'];

        //将$courseId对应的记录的course_club_id传过去，以便于自动选择
        $course_club_title = (M('Club')->select($course_club_id))['0']['club_title'];
        $this->assign('course_club_title',$course_club_title);

        $this->courseInfo = $Course->select($courseId);
        $this->display();
    }

    //显示课程详情+修改课程信息
    public function doCourseMod($courseId)
    {

        $Course = D('Course');

        $courseInfo = $Course->select($courseId);

        if (!$Course->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Course->getError());
        } else {
            // 创建成功，做其他事情
            if ($Course->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/coursepics/' . $courseInfo['0']['course_photo']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Course->course_photo = $pname;
                }
                $Course->course_id = $courseId;
                if ($Course->save() !== false) {
                    $this->success('修改成功', './courseList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }



    //文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/coursepics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }

<<<<<<< HEAD
    public function interfaceCourse()
    {
        //$id = I('get.id');

        $course = M('course');
        $result = $course->select();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

    public function interfaceCourseInfo()
    {
        $id=I('get.course_id');
        $course=M('course');
        $result = $course->where("course_id=$id")->find();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

=======
>>>>>>> 62e237362e20a94a945bc25068926c792b5c027b

}